export interface ILyricsItem {
  text: string;
  time: number;
}

export interface IComposeLyrics {
  text: string;
  c_text?: string;
  time: number;
}

export function parseLyric(lyricString: string): ILyricsItem[] {
  const lyricStrings = lyricString.split("\n");
  const timeRegExp = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/;
  const lyricInfos = [];
  for (const lineString of lyricStrings) {
    const timeResult = timeRegExp.exec(lineString) as any;
    if (!timeResult) continue;
    // 1.获取时间
    const minute = timeResult[1] * 60 * 1000;
    const second = timeResult[2] * 1000;
    const millsecondTime = timeResult[3];
    const millsecond = millsecondTime.length === 2 ? millsecondTime * 10 : millsecondTime * 1;
    const time = minute + second + millsecond;

    // 2.获取歌词文
    const text = lineString.replace(timeRegExp, "");
    text && lyricInfos.push({ time, text });
  }
  return lyricInfos;
}

export function composeCLyrics(music_list: ILyricsItem[], c_music_list: ILyricsItem[]): IComposeLyrics[] {
  const composeLyrics: IComposeLyrics[] = [];
  const bigger = music_list.length - c_music_list.length;
  music_list.forEach((item, index) => {
    if (item.text.length) {
      let composeItem: IComposeLyrics = {
        text: item.text,
        time: item.time,
        c_text: index + 1 > bigger ? c_music_list[index - bigger].text : "",
      };
      composeLyrics.push(composeItem);
    }
  });

  return composeLyrics;
}
